package tools;




public class LoginReplayPacket extends Packet {

	private  String name;
	private String pwd;
	private int flag; // flag = 1 表示登陆成功   flag = 0 表示登录失败
	public String getName() {
		return name;
	}
	public void setName(String name) {
		this.name = name;
	}
	public String getPwd() {
		return pwd;
	}
	public void setPwd(String pwd) {
		this.pwd = pwd;
	}
	public int getFlag() {
		return flag;
	}
	public void setFlag(int flag) {
		this.flag = flag;
	}

	@Override
	public byte[] serialize() {
		int totalLength = 16 + name.length() + pwd.length();
		byte[] buffer = new byte[totalLength];
		int offset = 0;
		//数据包的总长度
		byte[] totalLengthBuffer = Util.int2bytes(totalLength);
		System.arraycopy(totalLengthBuffer, 0, buffer, offset, 4);
		offset += 4;


		//数据包的类型
		byte[] packetTypeBuffer = Util.int2bytes(Service.PACKET_TYPE.S2C_LOGIN);
		System.arraycopy(packetTypeBuffer, 0, buffer, offset, 4);
		offset += 4;

		//flag 数据
		byte[] flagBuffer = Util.int2bytes(flag);
		System.arraycopy(flagBuffer, 0, buffer, offset, 4);
		offset += 4;

		//name数据的长度
		byte[] userNameLengthBuffer = Util.int2bytes(name.getBytes().length);
		System.arraycopy(userNameLengthBuffer, 0, buffer, offset, 4);
		offset += 4;

		// copy user name to buffer
		System.arraycopy(name.getBytes(), 0, buffer, offset, name.getBytes().length);
		offset += name.getBytes().length;

		// copy password to buffer
		System.arraycopy(pwd.getBytes(), 0, buffer, offset, pwd.getBytes().length);
		offset += pwd.getBytes().length;

		return buffer;

	}

	@Override
	public void unserialize(byte[] data) {
		System.out.println(data.length);
		// decode total length
		int totalLength = Util.bytes2int(data, 0);

		// decode user name length
		this.flag = Util.bytes2int(data, 8);

		int userNameLength = Util.bytes2int(data, 12);

		name = new String(data, 16, userNameLength);

		pwd = new String(data, 16 + userNameLength, totalLength - 16 - userNameLength);
	}

}
